package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.WaterMapper;
import com.village.zpark.pojo.Farmer;
import com.village.zpark.pojo.Water;
import com.village.zpark.service.WaterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
@Service
@Transactional
public class WaterServiceImpl implements WaterService{

    @Autowired
    private WaterMapper waterMapper;

    @Override
    public List<Water> findAll() {
        return waterMapper.selectAll();
    }

    @Override
    public Water findById(Integer id) {
        return waterMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addWater(Water water) {
        return waterMapper.insert(water);
    }

    @Override
    public int updateWater(Water water) {
        return waterMapper.updateByPrimaryKey(water);
    }

    @Override
    public int deleteWaterById(Integer id) {
        return waterMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Water> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Water.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("watertype", "%" + param + "%")
                    .orLike("quality", "%" + param + "%")
                    .orLike("treatmentprocess", "%" + param + "%"); // 使用 orLike 来实现多字段模糊查询
        }
        List<Water> waters;
        if (param != null && !param.isEmpty()) {
            waters = waterMapper.selectByExample(example);
        } else {
            waters = waterMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(waters);
    }
}
